var fileHost = 'https://qll-jcpt.oss-cn-hangzhou.aliyuncs.com/' // oss地址

require('./hmac.js')
require('./sha1.js')

const Crypto = require('./crypto.js')
const base64 = require('./base64.js')
const AccessKeySecret = 'n2IfxYSdexqHZsk7cYD3dSDCw9hz0H'
const timeout = 87600

const getSignature = function(policyBase64) {
  const accesskey = AccessKeySecret
  const bytes = Crypto.HMAC(Crypto.SHA1, policyBase64, accesskey, {
    asBytes: true
  })
  const signature = Crypto.util.bytesToBase64(bytes)
  return signature
}

const getPolicyBase64 = function() {
  const date = new Date()
  date.setHours(date.getHours() + timeout)
  const srcT = date.toISOString()
  const policyText = {
    expiration: srcT,
    // 设置该Policy的失效时间，超过这个失效时间之后，就没有办法通过这个policy上传文件了
    conditions: [
      ['content-length-range', 0, 100 * 1024 * 1024] // 设置上传文件的大小限制,5mb
    ]
  }
  const policyBase64 = base64.encode(JSON.stringify(policyText))
  return policyBase64
}

const policyBase64 = getPolicyBase64()
const signature = getSignature(policyBase64)

var config = {
  // aliyun OSS config
  uploadImageUrl: `${fileHost}`,
  AccessKeySecret: AccessKeySecret,
  // 登录oss控制台查看
  OSSAccessKeyId: 'LTAI5tRLpbyc8B7zZhAhEDEQ',
  signature: signature,
  policy: policyBase64,
  // 登录oss控制台查看
  timeout: timeout
}

module.exports = config
